package io.jee.runshan.service.impl;

import io.jee.medusa.orm.DaoSupport;
import io.jee.medusa.orm.condition.Condition;
import io.jee.medusa.orm.condition.ConditionGroup;
import io.jee.medusa.service.impl.BaseService;
import io.jee.medusa.vo.ResultBean;
import io.jee.runshan.dao.IProductDao;
import io.jee.runshan.dao.IShoppingCarDao;
import io.jee.runshan.dao.IShoppingCarProductDao;
import io.jee.runshan.domain.Product;
import io.jee.runshan.domain.ShoppingCar;
import io.jee.runshan.domain.ShoppingCarProduct;
import io.jee.runshan.service.IShoppingCarService;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

@Service
@Transactional
public class ShoppingCarServiceImpl extends BaseService<ShoppingCar> implements IShoppingCarService {
	@Resource
	private IShoppingCarDao carDao;
	@Resource
	private IShoppingCarProductDao carProductDao;
	@Resource
	private IProductDao productDao;

	@Override
	public ResultBean<?> addProduct(String wxOpenid, Integer productId, Integer count) {
		Product product = productDao.get(productId);
		String message = "添加商品成功";
		Boolean flag = true;
		if (product == null) {
			return ResultBean.result(false, "内部功能，非请勿入");
		}
		
		if (product.getStock() < count) {
			message = "库存不足";
			flag = false;
			count = product.getStock();
		}
		
		ConditionGroup group = new ConditionGroup();
		group.and(Condition.onNumber("car.wxOpenid").eq(wxOpenid));
		group.and(Condition.onNumber("product.id").eq(productId));
		ShoppingCarProduct carProduct = carProductDao.get(group);
		if (carProduct == null) {
			ShoppingCar car = carDao.unique("wxOpenid", wxOpenid);
			if (car == null) {
				car = new ShoppingCar();
				car.setWxOpenid(wxOpenid);
			}
			
			BigDecimal orderFullAmount = car.getFullAmount().add(product.getSalesPrice().multiply(new BigDecimal(count)));
			BigDecimal orderPreAmount = orderFullAmount.multiply(new BigDecimal(0.2));
			orderFullAmount.setScale(2, BigDecimal.ROUND_DOWN);
			orderPreAmount.setScale(2, BigDecimal.ROUND_DOWN);
			
			car.setFullAmount(orderFullAmount);
			car.setPreAmount(orderPreAmount);
			car.setProductCount(car.getProductCount() + 1);
			carDao.saveOrUpdate(car);
			
			carProduct = new ShoppingCarProduct();
			carProduct.setBuyCount(count);
			carProduct.setBuyPrice(product.getSalesPrice());
			carProduct.setCar(car);
			carProduct.setProduct(product);
			carProduct.setWxOpenid(wxOpenid);
			carProductDao.save(carProduct);
		} else {
			carProduct.setBuyCount(carProduct.getBuyCount() + count);
			carProductDao.update(carProduct);
			
			ShoppingCar car = carDao.unique("wxOpenid", wxOpenid);
			
			BigDecimal orderFullAmount = car.getFullAmount().add(product.getSalesPrice().multiply(new BigDecimal(count)));
			BigDecimal orderPreAmount = car.getFullAmount().multiply(new BigDecimal(0.2));
			orderFullAmount.setScale(2, BigDecimal.ROUND_DOWN);
			orderPreAmount.setScale(2, BigDecimal.ROUND_DOWN);
			
			car.setFullAmount(orderFullAmount);
			car.setPreAmount(orderPreAmount);
			carDao.update(car);
		}
		return ResultBean.result(flag, message, count);
	}

	
	@Override
	public ResultBean<?> removeProduct(String wxOpenid, Integer productId) {
		ConditionGroup group = new ConditionGroup();
		group.and(Condition.onNumber("car.wxOpenid").eq(wxOpenid));
		group.and(Condition.onNumber("product.id").eq(productId));
		ShoppingCarProduct carProduct = carProductDao.get(group);
		if (carProduct != null) {
			carProductDao.delete(carProduct);
			ShoppingCar car = carDao.unique("wxOpenid", wxOpenid);
			car.clearProduct();
			List<ShoppingCarProduct> carProducts = carProductDao.list("car.wxOpenid", wxOpenid);
			for (ShoppingCarProduct cp : carProducts) {
				car.addPrudoct(cp);
			}
			carDao.update(car);
		}
		return ResultBean.result(true, "删除商品成功");
	}


	@Override
	public ResultBean<?> addCount(String wxOpenid, Integer productId) {
		String message = "增加商品数量成功";
		Boolean flag = true;
		ConditionGroup group = new ConditionGroup();
		group.and(Condition.onNumber("car.wxOpenid").eq(wxOpenid));
		group.and(Condition.onNumber("product.id").eq(productId));
		ShoppingCarProduct carProduct = carProductDao.get(group);
		ShoppingCar car = carProduct.getCar();
		Integer stock = 0;
		List<ShoppingCarProduct> carProducts = carProductDao.list("car.wxOpenid", wxOpenid);
		car.clearProduct();
		for (ShoppingCarProduct cp : carProducts) {
			if (cp.getProduct().getId() == productId) {
				stock = cp.getProduct().getStock();
				if (stock > 0) {
					cp.setBuyCount(cp.getBuyCount() + 1);
					carProductDao.update(cp);
				} else {
					message = "库存不足";
					flag = false;
				}
			}
			car.addPrudoct(cp);
		}
		carDao.update(car);
		return ResultBean.result(flag, message, stock);
	}


	@Override
	public ResultBean<?> reduceCount(String wxOpenid, Integer productId) {
		ConditionGroup group = new ConditionGroup();
		group.and(Condition.onNumber("car.wxOpenid").eq(wxOpenid));
		group.and(Condition.onNumber("product.id").eq(productId));
		ShoppingCarProduct carProduct = carProductDao.get(group);
		ShoppingCar car = carProduct.getCar();
		List<ShoppingCarProduct> carProducts = carProductDao.list("car.wxOpenid", wxOpenid);
		car.clearProduct();
		for (ShoppingCarProduct cp : carProducts) {
			if (cp.getProduct().getId() == productId) {
				cp.setBuyCount(cp.getBuyCount() - 1);
				carProductDao.update(cp);
			}
			car.addPrudoct(cp);
		}
		carDao.update(car);
		return ResultBean.result(true, "减少商品数量成功");
	}
	
	@Override
	public ResultBean<?> changeCount(String wxOpenid, Integer productId, Integer count) {
		String message = "修改商品数量成功";
		Boolean flag = true;
		ConditionGroup group = new ConditionGroup();
		group.and(Condition.onNumber("wxOpenid").eq(wxOpenid));
		group.and(Condition.onNumber("product.id").eq(productId));
		ShoppingCarProduct carProduct = carProductDao.get(group);
		
		Integer stock = carProduct.getProduct().getStock();
		Integer alreadyBuy = carProduct.getBuyCount();
		Boolean isIncrease = true; // 是否是增加数量
		if (count - alreadyBuy < 0) {
			isIncrease = false;
		}
		
		if (isIncrease && stock < count - alreadyBuy ) {
			message = "库存不足";
			flag = false;
			count = stock;
		}
		
		carProduct.setBuyCount(count);
		carProductDao.update(carProduct);
		
		List<ShoppingCarProduct> carProducts = carProductDao.list("wxOpenid", wxOpenid);
		
		ShoppingCar car = carDao.unique("wxOpenid", wxOpenid);
		car.clearProduct();
		
		for (ShoppingCarProduct cp : carProducts) {
			car.addPrudoct(cp);
		}
		carDao.update(car);
		return ResultBean.result(flag, message, count);
	}

	@Override
	public DaoSupport<ShoppingCar> getDao() {
		return carDao;
	}

}
